home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / LoadImg.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-25  |  6.9 KB  |  205 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmLoadImg 
  4.    Caption         =   "LoadImg []"
  5.    ClientHeight    =   3915
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   6930
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   3915
  11.    ScaleWidth      =   6930
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.CommandButton cmdRefresh 
  14.       Caption         =   "Refresh"
  15.       Height          =   375
  16.       Left            =   1440
  17.       TabIndex        =   4
  18.       Top             =   60
  19.       Width           =   855
  20.    End
  21.    Begin MSComDlg.CommonDialog dlgOpenFile 
  22.       Left            =   2760
  23.       Top             =   0
  24.       _ExtentX        =   847
  25.       _ExtentY        =   847
  26.       _Version        =   393216
  27.    End
  28.    Begin VB.TextBox txtScale 
  29.       Height          =   285
  30.       Left            =   600
  31.       TabIndex        =   3
  32.       Text            =   "0.5"
  33.       Top             =   120
  34.       Width           =   735
  35.    End
  36.    Begin VB.PictureBox picStretched 
  37.       AutoRedraw      =   -1  'True
  38.       BorderStyle     =   0  'None
  39.       Height          =   1815
  40.       Left            =   840
  41.       ScaleHeight     =   1815
  42.       ScaleWidth      =   1695
  43.       TabIndex        =   1
  44.       Top             =   480
  45.       Visible         =   0   'False
  46.       Width           =   1695
  47.    End
  48.    Begin VB.PictureBox picAntiAliased 
  49.       AutoRedraw      =   -1  'True
  50.       BorderStyle     =   0  'None
  51.       Height          =   2400
  52.       Left            =   3360
  53.       ScaleHeight     =   2400
  54.       ScaleWidth      =   2400
  55.       TabIndex        =   0
  56.       Top             =   480
  57.       Visible         =   0   'False
  58.       Width           =   2400
  59.    End
  60.    Begin VB.Label Label1 
  61.       Caption         =   "Scale"
  62.       Height          =   255
  63.       Index           =   1
  64.       Left            =   120
  65.       TabIndex        =   2
  66.       Top             =   120
  67.       Width           =   615
  68.    End
  69.    Begin VB.Image imgOriginal 
  70.       Height          =   600
  71.       Left            =   120
  72.       Top             =   480
  73.       Visible         =   0   'False
  74.       Width           =   600
  75.    End
  76.    Begin VB.Menu mnuFile 
  77.       Caption         =   "&File"
  78.       Begin VB.Menu mnuFileOpen 
  79.          Caption         =   "&Open..."
  80.          Shortcut        =   ^O
  81.       End
  82.    End
  83. Attribute VB_Name = "frmLoadImg"
  84. Attribute VB_GlobalNameSpace = False
  85. Attribute VB_Creatable = False
  86. Attribute VB_PredeclaredId = True
  87. Attribute VB_Exposed = False
  88. Option Explicit
  89. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  90. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  91. Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
  92. Private Const LR_LOADFROMFILE = &H10&
  93. Private Const IMAGE_BITMAP = 0
  94. Private Const IMAGE_ICON = 1
  95. Private Const IMAGE_CURSOR = 2
  96. Private FileName As String
  97. ' Arrange the controls.
  98. Private Sub ArrangeControls(ByVal picture_scale As Single)
  99. Dim wid As Single
  100. Dim hgt As Single
  101.     ' Position the image controls.
  102.     picStretched.Move _
  103.         imgOriginal.Left + imgOriginal.Width + 120, _
  104.         imgOriginal.Top, _
  105.         imgOriginal.Width * picture_scale, _
  106.         imgOriginal.Height * picture_scale
  107.     picAntiAliased.Move _
  108.         picStretched.Left + picStretched.Width + 120, _
  109.         imgOriginal.Top, _
  110.         imgOriginal.Width * picture_scale, _
  111.         imgOriginal.Height * picture_scale
  112.     ' Make the form big enough.
  113.     wid = picAntiAliased.Left + picAntiAliased.Width
  114.     Width = wid + Width - ScaleWidth + 120
  115.     hgt = picAntiAliased.Top + picAntiAliased.Height
  116.     If hgt < imgOriginal.Top + imgOriginal.Height _
  117.         Then hgt = imgOriginal.Top + imgOriginal.Height
  118.     Height = hgt + Height - ScaleHeight + 120
  119. End Sub
  120. ' Load a picture into a PictureBox using LoadImage.
  121. Private Sub LoadImageFile(ByVal pic As PictureBox, ByVal file_name As String)
  122. Dim wid As Long
  123. Dim hgt As Long
  124. Dim hbmp As Long
  125. Dim image_hdc As Long
  126.     ' Get the PictureBox's dimensions in pixels.
  127.     wid = pic.ScaleX(pic.ScaleWidth, pic.ScaleMode, vbPixels)
  128.     hgt = pic.ScaleY(pic.ScaleHeight, pic.ScaleMode, vbPixels)
  129.     ' Load the bitmap.
  130.     hbmp = LoadImage(0, file_name, IMAGE_BITMAP, _
  131.         wid, hgt, LR_LOADFROMFILE)
  132.     ' Make the picture box display the image.
  133.     SelectObject pic.hdc, hbmp
  134.     ' Destroy the bitmap to free its resources.
  135.     DeleteObject hbmp
  136.     ' Refresh the image.
  137.     pic.Refresh
  138. End Sub
  139. ' Display the images.
  140. Private Sub DisplayImages(ByVal file_name As String)
  141. Dim picture_scale As Single
  142.     ' Do nothing if no picture is loaded.
  143.     If Len(FileName) = 0 Then Exit Sub
  144.     ' Get the scale.
  145.     On Error Resume Next
  146.     picture_scale = CSng(txtScale.Text)
  147.     If Err.Number <> 0 Then picture_scale = 1
  148.     On Error GoTo LoadError
  149.     ' Load the file at normal scale using LoadPicture.
  150.     imgOriginal.Picture = LoadPicture(file_name)
  151.     ' Arrange the controls.
  152.     ArrangeControls picture_scale
  153.     ' Stretch the image using PaintPicture.
  154.     picStretched.Cls
  155.     picStretched.PaintPicture imgOriginal.Picture, _
  156.         0, 0, picStretched.Width, picStretched.Height
  157.     ' Load the file using LoadImage.
  158.     picAntiAliased.Cls
  159.     LoadImageFile picAntiAliased, file_name
  160.     imgOriginal.Visible = True
  161.     picStretched.Visible = True
  162.     picAntiAliased.Visible = True
  163.     Exit Sub
  164. LoadError:
  165.     MsgBox "Error " & Format$(Err.Number) & vbCrLf & _
  166.         " opening file '" & FileName & "'" & vbCrLf & _
  167.         Err.Description
  168. End Sub
  169. ' Redisplay the images.
  170. Private Sub cmdRefresh_Click()
  171.     DisplayImages FileName
  172. End Sub
  173. ' Start in the current directory.
  174. Private Sub Form_Load()
  175.     dlgOpenFile.CancelError = True
  176.     dlgOpenFile.InitDir = App.Path
  177.     dlgOpenFile.Filter = _
  178.         "Bitmaps (*.bmp)|*.bmp|" & _
  179.         "Icons (*.ico)|*.ico|" & _
  180.         "Cursors (*.cur)|*.cur|" & _
  181.         "Graphic Files|*.bmp;*.ico;*.cur|" & _
  182.         "All Files (*.*)|*.*"
  183. End Sub
  184. ' Load the indicated file.
  185. Private Sub mnuFileOpen_Click()
  186.     ' Let the user select a file.
  187.     On Error Resume Next
  188.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  189.     dlgOpenFile.ShowOpen
  190.     If Err.Number = cdlCancel Then
  191.         Exit Sub
  192.     ElseIf Err.Number <> 0 Then
  193.         Beep
  194.         MsgBox "Error selecting file.", , vbExclamation
  195.         Exit Sub
  196.     End If
  197.     On Error GoTo 0
  198.     FileName = Trim$(dlgOpenFile.FileName)
  199.     dlgOpenFile.InitDir = Left$(FileName, Len(FileName) _
  200.         - Len(dlgOpenFile.FileTitle) - 1)
  201.     Caption = "LoadImg [" & dlgOpenFile.FileTitle & "]"
  202.     ' Display the images.
  203.     DisplayImages FileName
  204. End Sub
  205.